package site.xuqing.androidbase.core;

import android.content.Intent;
import android.os.Bundle;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import androidx.annotation.CallSuper;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import site.xuqing.androidbase.util.LogUtil;

/**
 * @author xuqing
 * @Description BaseActivity
 * @createTime 2021年12月10日 10:49:05
 */
public abstract class BaseActivity extends AppCompatActivity implements IBaseActivity,IBaseContext {
    private final List<BaseActivity> mBaseActivities = new ArrayList<>();
    
    @Override
    @CallSuper
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayout());
        initView(savedInstanceState);
        initData();
        initEvent();
        mBaseActivities.add(this);
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        LogUtil.logActivity(getClass().getSimpleName());
    }
    
    @Override
    @CallSuper
    protected void onDestroy() {
        super.onDestroy();
        mBaseActivities.remove(this);
    }
    
    public List<BaseActivity> getBaseActivities() {
        return mBaseActivities;
    }
    
    @Override
    public void closeActivity() {
        finish();
    }
    
    @Override
    public void closeAllActivities() {
        Iterator<BaseActivity> iterator = mBaseActivities.iterator();
        while (iterator.hasNext()) {
            BaseActivity activity = iterator.next();
            if (activity != null) {
                activity.finish();
                iterator.remove();
            }
        }
    }
    
    @Override
    public void startActivity(Class<? extends BaseActivity> activity) {
        startActivity(new Intent(this, activity));
    }
    
    @Override
    public void startActivity(Class<? extends BaseActivity> activity, Bundle bundle) {
        startActivity(new Intent(this, activity).putExtras(bundle));
    }
    
    @Override
    public void startActWithIntent(Intent intent){
        startActivity(intent);
    }
    
    @Override
    public void startActivityForResult(Class<? extends BaseActivity> activity, int requestCode) {
        startActivityForResult(new Intent(this, activity), requestCode);
    }
    
    @Override
    public void startActivityForResult(Class<? extends BaseActivity> activity, Bundle bundle, int requestCode) {
        startActivityForResult(new Intent(this, activity).putExtras(bundle), requestCode);
    }
    
    @Override
    public void startActWithIntentForResult(Intent intent, int requestCode){
        startActivityForResult(intent,requestCode);
    }
}
